package com.quanyan.user.web;

import com.quanyan.api.APIResponse;
import com.quanyan.common.user.AppUserCacheInfo;
import com.quanyan.redisCluster.config.RedisTemplate;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.user.service.DiscoveryVideoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by huadongyang on 2017/3/1.
 */
@RestController
@RequestMapping(value = "/api/user/discoveryVideo", method = RequestMethod.POST, produces = "application/json")
public class DiscoveryVideoController {
    private static final Logger logger = LoggerFactory.getLogger(DiscoveryVideoController.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DiscoveryVideoService discoveryVideoService;

    @RequestMapping(value = "/getDiscoveryVideoDetail")
    public APIResponse getDiscoveryVideoDetail(@RequestBody Map map, HttpServletRequest request) {
        try {
            String userCookiesName = request.getHeader("userCookiesName");
            RedisKey redisKey = new RedisKey(userCookiesName, "");
            AppUserCacheInfo appUserCacheInfo = (AppUserCacheInfo) redisTemplate.getObjectFromC(redisKey, AppUserCacheInfo.class);
            Integer uid = null;
            if (appUserCacheInfo != null)
                uid = appUserCacheInfo.getUser_id();

            Object obj = map.get("videoId");
            if (obj == null) {
                return APIResponse.returnFail("请传入有效参数！");
            }
            Integer videoId = Integer.parseInt(obj.toString());
            return discoveryVideoService.getDiscoveryVideoDetail(uid, videoId);
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail("服务器异常！");
        }
    }

    @RequestMapping(value = "/getVideoDetails")
    public APIResponse getVideoDetails(@RequestBody Map map, HttpServletRequest request) {
        try {
            List<Integer> videoIds = (List<Integer>)map.get("videoIds");
            if (videoIds == null || videoIds.size() <= 0 ) {
                return APIResponse.returnFail("请传入有效参数！");
            }
            return discoveryVideoService.getVideoDetails(videoIds);
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail("服务器异常！");
        }
    }
}
